import { pinyin } from 'pinyin-pro';
export const pinyinFilter = function (text) {
    if (!text) {
        return '';
    } else {
        const regPos = /^\d+(\.\d+)?$/;
        const letter = pinyin(text, { pattern: 'first', toneType: 'none' })[0];
        return regPos.test(letter) === true ? letter : letter.toUpperCase();
    }
};
export const colorFilter = (letter, colorList) => {
    let color = '';
    Object.keys(colorList).forEach((key) => {
        if (colorList[key].letter.includes(letter) === true) {
            color = colorList[key].color;
        }
    });
    return color;
};
/**
 * @description: 输入框input事件·防抖  适用情景：输入框、搜索框input事件，等待一次性输入完后请求
 * @return {*} fn 函数
 * @return {*} delay :延迟时间/秒
 * @return {*}
 *  调用:
  1.import 引入
  2.throttle(input事件,500)
 */

export function bounce(fn, delay, ctx = window) {
    let timer = null;
    return function (...args) {
        return new Promise((resolve) => {
            if (timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(() => {
                if (fn instanceof Promise) {
                    fn.apply(ctx, args).then((res) => {
                        resolve(res);
                    });
                } else {
                    resolve(fn.apply(ctx, args));
                }
            }, delay);
        });
    };
}
